<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="${ctx }/static/js/jquery.js"></script>
<title>关键词申请</title>
    <style>
/*         关键词申请 */
		.custDiv{
			width:510px;border: 1px solid;
			position:absolute;left: 130px;z-index: 2;
			background-color: white;
			display: none;
		}
		.keyWord{
			border: 1px solid;margin-top: 20px;
		}
		.keyWord form{
			margin-top: 20px;
		}
		.keyWord .layui-input,input[name=search]{
			width:500px;
			height:30px;
			display: inline-block;
		}
		
		
		#custlist{
		
			float:left;border:1px solid;width:100px;height:50px;
			cursor:pointer;
		}
		.keywordBtn{
			margin-left:20px;
			margin-bottom:20px;
		}
    </style>
</head>
<body>

<!-- 	关键词申请页面 -->
	<div id="keywordDiv">
		
		<div class="layui-form-item">
    		<label class="layui-form-label">客户名称：</label>
    		<div class="layui-input-block">
    				
      			<input onchange="search()" type="text" name="search"  class="layui-input">
   			 	<span>【当前账户余额：￥】</span><br>
   			 </div>
  		</div>
		
		<div class="custDiv">
			
		</div>
		<div class="keyWord">
			<form id="keyForm" action="{ctx}/keyWord/kwInsert" method="post">
				<input name ="agentAccount"  type="hidden" value="${user.loginAccount}">
				<input name ="agent"  type="hidden" value="${user.loginName}">
				<div>
					 <div class="layui-form-item">
			    		<label class="layui-form-label">客户名称：</label>
			    		<div class="layui-input-block">
			      			<input type="text" name="customerName" class="layui-input">
			   			 </div>
			  		</div>
				</div>
				<div>
					 <div class="layui-form-item">
			    		<label class="layui-form-label">关键词：</label>
			    		<div class="layui-input-block">
			      			<input  type="text" name="keywordName" class="layui-input"><span class="key" style="color:red"></span>
			   			 </div>
			  		</div>
				</div>
				
				<div class="layui-form-item">
				    <label class="layui-form-label">服务类别：</label>
				    <div onclick="serviceChange()" class="layui-input-block">
				      <select class="change" name="serviceType">
							<option value = "">-请选择-</option>
				      </select>
				  	</div>
				 </div>
				 <div class="layui-form-item">
				    <label class="layui-form-label">服务年限：</label>
				    <div onclick="serviceChange()" class="layui-input-block">
				      <select class="change"  name="year">
				        <option value="0">-请选择-</option>
				   
				   
				      </select>
				    </div>
				 </div>
				 <div>
					 <div class="layui-form-item">
			    		<label class="layui-form-label">价格：</label>
			    		<div class="layui-input-block">
			      			<input type="text" disabled name="price"  class="layui-input"><span class="priceSpan"></span>
			   			 </div>
			  		</div>
				</div>
				 <input class="keywordBtn" onclick="keyWordapplyFor()" type="button" value="提交申请">
			</form>
		</div>
	</div>
</body>

<script>
	
	$("input[name=search]").click(function(){
		
		search();
	})

//  客户搜索
	function search(){
		var custName = $("input[name=search]").val();
		var agent = $("#agent1").val();
		$.ajax({
			url:"${ctx}/custom/custList?companyName="+custName+"&agent="+agent+"",
			type:"post",
			success:function(data){
				$(".custDiv").show();
				var company = data;
				var html="";
				for(var i = 0;i<company.length;i++){
					html +='<div id="custlist" >客户类型:'+company[i].companyType+'<br>'+'<p onclick="aa(this)">'+company[i].companyName+'</p>'+'</div>'		
				}
				$(".custDiv").html(html)
			}
		})
		
		//  服务类型	
		$.ajax({
			url:"${ctx}/serviceType/type",
			type:"post",
			success:function(data){
				var op = data;
				var html = '<option value=0>-请选择-</option>';
				for(var i=0;i<op.length;i++){
					html+="<option value="+op[i].serviceCost+">"+op[i].serviceType+"</option>";
				}
				$("select[name=serviceType]").html(html);
			}
		})
		// 	服务年限
		var ht="";
		$.ajax({
			url:"${ctx}/serviceYear/year",
			type:"post",
			success:function(data){
				
				var op =  parseInt(data);
				
				var html = '<option>-请选择-</option>';
	
				for(var i=1;i<=op;i++){
 					html+='<option vaule='+i+'>'+i+'</option>';
					
 				}
				//优惠类型
				$.ajax({
					url:"${ctx}/youhui/type",
					type:"post",
					success:function(data){
						var op = data;
						for(var i=0;i<op.length;i++){
							html+="<option value="+op[i].number+">"+op[i].name+"</option>";
						}
						$("select[name=year]").html(html);
					}
				})
			}
		})
		
		
		$.ajax({
			url:"${ctx}/youhui/type",
			type:"post",
			success:function(data){
				var op = data;
				for(var i=0;i<op.length;i++){
					ht+="<option value="+op[i].number+">"+op[i].name+"</option>";
				}
				$("select[name=year]").html(ht);
			}
		})
    }	
    
// 查找客户点击事件
	function aa(obj){

    	var custName = $(obj).html();
    	$(".custDiv").hide()	
    	$("input[name=customerName]").val(custName);
    	$("input[name=search]").val(custName);
    }

	function serviceChange(){
		//计算关键词价格

		$(".change").change(function(){
			
			var serviceType = $("select[name=serviceType]").val();
			
			var year = $("select[name=year]").val();
			
			var price= serviceType*year;
			
			if(serviceType >0 && year>0){
				$("input[name=price]").val(price);
			}else{
				$("input[name=price]").val('');
				
			}
			
		})
		
	}
	
	
	function keyWordapplyFor(){
		
		var keywordName = $("input[name=keywordName]").val();
	
		var customName =$("input[name=customerName]").val();
		
		var agent = $("#agent1").val();
		
		var year = $("select[name=year]").val();
		
		var serviceType = $("select[name=serviceType]").find("option:selected").html();
		
		var price = $("input[name=price]").val();
		
		var flag = false;
		
		if(keywordName ==""||keywordName==null){
			$(".key").text("关键词不能为空")	
			return;
		}else{
			
			$.ajax({
				url:"${ctx}/keyWord/kwSearch?keyWord="+keywordName,
				type:"get",
				async:false,
				success:function(data){
					
					if(data ==1){	
						$(".key").text("关键词已被使用，请更换");	
						flag = true;
					}
				}
			})
		}
		
		if(price==""){
			
			$(".priceSpan").text("价格不能为空");
			flag = true;
			return;
		}
		
		var keyWord = {
				keywordName:keywordName,
				customName:customName,
		//		user:user,
				year:year,
				serviceType:serviceType,
				price:price,
		}
		if(!flag){
			$.ajax({
				url:"${ctx}/keyWord/kwInsert/"+agent,
				type:"post",
				async:false,
				data:keyWord,
				success:function(data){
					if(data==1){
						$("#changeDiv").html("申请成功，可到关键词管理界面查看");
						setTimeout(function(){$("#changeDiv").html("")},3000)
					}
				}
			})
			
		}
		
	}
	
	
</script>
</html>

























